package com.yongninger.activity.user;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.yongninger.activity.LoginActivity;
import com.yongninger.activity.MainActivity;
import com.yongninger.activity.R;
import com.yongninger.dao.UserDao;

public class ModifyPwdActivity extends Activity {
	private UserDao userDao;
	private Button modifybtn;
	private EditText upwd1;
	private EditText upwd2;
	private EditText upwd3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_modifypwd);
		userDao = new UserDao(getApplicationContext());
		upwd1 = (EditText) findViewById(R.id.upwd1);
		upwd2 = (EditText) findViewById(R.id.upwd2);
		upwd3 = (EditText) findViewById(R.id.upwd3);
		modifybtn = (Button) findViewById(R.id.modify);
		OnClickListener onClickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				boolean temp = true;
				SharedPreferences userInfo = getSharedPreferences("user_info",
						0);
				if (upwd1.length() <= 0) {
					Toast.makeText(getApplicationContext(), "原始密码不能为空",
							Toast.LENGTH_SHORT).show();
					temp = false;
					return;
				} else {
					if (!userInfo.getString("upwd", "").equals(
							upwd1.getText().toString())) {
						Toast.makeText(getApplicationContext(), "原始密码不正确",
								Toast.LENGTH_SHORT).show();
						temp = false;
						return;
					}
				}
				if (upwd2.length() <= 0) {
					Toast.makeText(getApplicationContext(), "新密码不能为空",
							Toast.LENGTH_SHORT).show();
					temp = false;
					return;
				} else {
					if (!upwd3.getText().toString()
							.equals(upwd2.getText().toString())) {

						Toast.makeText(getApplicationContext(), "新密码和确认密码不一致",
								Toast.LENGTH_SHORT).show();
						temp = false;
						return;
					}
				}
				if (upwd3.length() <= 0) {
					Toast.makeText(getApplicationContext(), "确认密码不能为空",
							Toast.LENGTH_SHORT).show();
					temp = false;
					return;
				}
				if (temp) {
					userInfo.edit().putString("upwd", upwd2.getText().toString()).commit();
					userDao.modifyPwd(upwd2.getText().toString(),userInfo.getInt("uid", 0));
					Intent intent = new Intent(ModifyPwdActivity.this,
							LoginActivity.class);
					startActivity(intent);
					Toast.makeText(getApplicationContext(), "密码修改成功",
							Toast.LENGTH_SHORT).show();
				}
			}
		};
		modifybtn.setOnClickListener(onClickListener);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			// 跳转到查询页面
			Intent intent = new Intent(ModifyPwdActivity.this,
					MainActivity.class);
			startActivity(intent);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
